package com.example.weather_app.diary;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class DiaryDao {
    private final DiaryDBHelper dbHelper;

    public DiaryDao(Context context) {
        this.dbHelper = new DiaryDBHelper(context);
    }

    // 添加日记
    public long addDiary(Diary diary) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();

        if (diary.getId() == null) {
            diary.setId(UUID.randomUUID().toString());
        }

        values.put(DiaryDBHelper.COL_ID, diary.getId());
        values.put(DiaryDBHelper.COL_TITLE, diary.getTitle());
        values.put(DiaryDBHelper.COL_CONTENT, diary.getContent());
        values.put(DiaryDBHelper.COL_DATE, diary.getDate());
        values.put(DiaryDBHelper.COL_REMINDER_TIME, diary.getReminderTime());  // 新增

        long rowId = db.insert(DiaryDBHelper.TABLE_NAME, null, values);
        db.close();
        return rowId;
    }

    // 获取所有日记
    public List<Diary> getAllDiaries() {
        List<Diary> diaryList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + DiaryDBHelper.TABLE_NAME +
                " ORDER BY " + DiaryDBHelper.COL_CREATED_AT + " DESC";

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Diary diary = new Diary();
                diary.setId(cursor.getString(0));
                diary.setTitle(cursor.getString(1));
                diary.setContent(cursor.getString(2));
                diary.setDate(cursor.getString(3));
                diary.setReminderTime(cursor.getString(4));  // 新增
                // 处理created_at时间戳
                diaryList.add(diary);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return diaryList;
    }

    // 根据ID获取日记
    public Diary getDiaryById(String id) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(
                DiaryDBHelper.TABLE_NAME,
                new String[]{
                        DiaryDBHelper.COL_ID,
                        DiaryDBHelper.COL_TITLE,
                        DiaryDBHelper.COL_CONTENT,
                        DiaryDBHelper.COL_DATE,
                        DiaryDBHelper.COL_REMINDER_TIME,  // 新增
                        DiaryDBHelper.COL_CREATED_AT
                },
                DiaryDBHelper.COL_ID + "=?",
                new String[]{id},
                null, null, null, null
        );

        Diary diary = null;
        if (cursor != null && cursor.moveToFirst()) {
            diary = new Diary();
            diary.setId(cursor.getString(0));
            diary.setTitle(cursor.getString(1));
            diary.setContent(cursor.getString(2));
            diary.setDate(cursor.getString(3));
            diary.setReminderTime(cursor.getString(4));  // 新增
            // 处理created_at时间戳
            cursor.close();
        }

        db.close();
        return diary;
    }

    // 更新日记
    public int updateDiary(Diary diary) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DiaryDBHelper.COL_TITLE, diary.getTitle());
        values.put(DiaryDBHelper.COL_CONTENT, diary.getContent());
        values.put(DiaryDBHelper.COL_DATE, diary.getDate());
        values.put(DiaryDBHelper.COL_REMINDER_TIME, diary.getReminderTime());  // 新增

        int rowsAffected = db.update(
                DiaryDBHelper.TABLE_NAME,
                values,
                DiaryDBHelper.COL_ID + " = ?",
                new String[]{diary.getId()}
        );

        db.close();
        return rowsAffected;
    }

    // 删除日记
    public void deleteDiary(String id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(
                DiaryDBHelper.TABLE_NAME,
                DiaryDBHelper.COL_ID + " = ?",
                new String[]{id}
        );
        db.close();
    }

    // 获取所有设置了提醒的日记
    public List<Diary> getReminderDiaries() {
        List<Diary> diaryList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + DiaryDBHelper.TABLE_NAME +
                " WHERE " + DiaryDBHelper.COL_REMINDER_TIME + " IS NOT NULL" +
                " ORDER BY " + DiaryDBHelper.COL_REMINDER_TIME + " ASC";

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Diary diary = new Diary();
                diary.setId(cursor.getString(0));
                diary.setTitle(cursor.getString(1));
                diary.setContent(cursor.getString(2));
                diary.setDate(cursor.getString(3));
                diary.setReminderTime(cursor.getString(4));
                diaryList.add(diary);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return diaryList;
    }
    public List<Diary> getDiariesByDate(String date) {
        List<Diary> diaryList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + DiaryDBHelper.TABLE_NAME +
                " WHERE " + DiaryDBHelper.COL_DATE + " = ?" +
                " ORDER BY " + DiaryDBHelper.COL_CREATED_AT + " DESC";

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, new String[]{date});

        if (cursor.moveToFirst()) {
            do {
                Diary diary = new Diary();
                diary.setId(cursor.getString(0));
                diary.setTitle(cursor.getString(1));
                diary.setContent(cursor.getString(2));
                diary.setDate(cursor.getString(3));
                diary.setReminderTime(cursor.getString(4));
                diaryList.add(diary);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return diaryList;
    }
}